<?php
/**
 * Core_Cache
 * 
 * @author naspeh
 * @version $Id: Cache.php 662 2008-06-25 20:16:56Z naspeh $
 */
class Core_Cache 
{
    /**
     * Опции
     *
     * @var Zend_Config
     */
    protected static $_options = array(
        'enabled' => true
    );
    /**
     * Возвращает объект кэша
     *
     * @param  string $label
     * @return Zend_Cache_Core 
     */
    public static function get($label = 'default')
    {
        $label = (string) $label;
        try {
            $config = Core_Config::get('cache|' . $label);
        } catch (Exception $e) {
            throw $e;
        }
        if (isset($config['frontend']) && isset($config['backend']) 
            && isset($config['frontend']['name']) && isset($config['frontend']['options'])
                && isset($config['backend']['name']) && isset($config['backend']['options'])) {
            if (!isset($config['frontend']['options']['caching'])) {
                $config['frontend']['options']['caching'] = self::getOptions()->enabled;
            }
        	return Zend_Cache::factory(
        	   $config['frontend']['name'], 
        	   $config['backend']['name'], 
        	   $config['frontend']['options'], 
        	   $config['backend']['options']
        	);
        } else {
        	throw new Core_Exception('badConfigFormat# label: ' . $label);
        }
    }
    /**
     * Устанавливает опции
     *
     * @param Zend_Config|array $options
     */
    public static function setOptions($options)
    {
        if ($options instanceof Zend_Config) {
            self::$_options = $options->toArray();
        } else {
            self::$_options = $options;
        }
    }
    /**
     * Возвращает опции
     *
     * @return Zend_Config
     */
    public static function getOptions()
    {
        $options = new Zend_Config(self::$_options);      
        return $options;
    } 
}